package tgc.edu.fhh.web.controller;



import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.TablePageable;
import tgc.edu.fhh.entity.Course;
import tgc.edu.fhh.service.CourseService;
import tgc.edu.fhh.web.form.CourseForm;

@RestController
@RequestMapping(value = "/course")
public class CourseController {
	@Autowired
	private CourseService couservice;
	
	@RequestMapping(value = "/page")
	public Object page(TablePageable page,CourseForm form) {
		PageRequest pagerequest = page.buildPageRequest();
		Specification<Course> spec = buildSpec(form);
		Page<Course> page2 = couservice.findAll(spec,pagerequest);
		return new AjaxResult(AjaxResult.buildTableResult(page2));
	}
	
	@RequestMapping(value = {"/del"},method = RequestMethod.POST)
	public void del(Integer id) {
		couservice.deleteById(id);
	}
	
	@RequestMapping(value = "/edit")
	public void update(Integer id,ModelMap modelmap) {
		Course course = new Course();
		if (id!=null) {
			course = couservice.findById(id);
		}
		modelmap.put("model", course);
	}
	
	@RequestMapping(value = {"/save"},method = RequestMethod.POST)
	public Object save(CourseForm form) {
		try {
			Course course = new Course();
			Integer id = form.getId();
			if (id!=null) {
				course = couservice.findById(id);
			}
			BeanUtils.copyProperties(form, course,"id");
			couservice.save(course);
			return new AjaxResult("数据保存成功");
		}catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}
	
	@RequestMapping(value = "/search")
	public void search(CourseForm form,ModelMap modelmap) {
		modelmap.put("model", form);
	}
	

	@SuppressWarnings("serial")
	private Specification<Course> buildSpec(CourseForm form) {
		Specification<Course> spec = new Specification<Course>() {
			
			@Override
			public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String search = form.getSearch();
				if (StringUtils.hasText(search)) {
					HashSet<Predicate> rules2 = new HashSet<>();
					rules2.add(cb.like(root.get("courseName"),"%"+search+"%" ));
					rules2.add(cb.like(root.get("courseHour"),"%"+search+"%" ));
					rules.add(cb.or(rules2.toArray(new Predicate[rules2.size()])));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
}
